//
//  YHYStyleTableViewCell.m
//  Home
//
//  Created by 杨泓晔 on 16/5/12.
//  Copyright © 2016年 LanJue. All rights reserved.
//

#import "YHYStyleTableViewCell.h"
#import "YHYDetailPictureCollectionViewCell.h"
@interface YHYStyleTableViewCell ()<UICollectionViewDataSource,UICollectionViewDelegate>{
    NSInteger index;
    CGFloat _itemWH;
    CGFloat _margin;
    
}
@property (nonatomic, strong)UILabel *xmLabel;
@property (nonatomic, strong)UICollectionViewFlowLayout *layout;
@property (nonatomic, strong)UICollectionView *myCollectionView;
@property (nonatomic, strong)NSMutableArray *arr;
@property (nonatomic, strong)NSMutableArray *keyArray;
@property (nonatomic, strong)NSMutableArray *didArray;
@property (nonatomic, strong)NSMutableArray *keyStrArray;
//@property (nonatomic, strong)NSMutableArray *didstrArray;
@end

@implementation YHYStyleTableViewCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self creatView];
        
        
    }
    return self;
}


- (void)creatView{
    //名字
    self.xmLabel = [[UILabel alloc]init];
    //self.xmLabel.textColor = HS;
    self.xmLabel.textColor = LINE;
    self.xmLabel.font = [UIFont fontWithName:TFont size:TEXTCONTENTFONT2 * AUTO_WIDTH];
    
    [self.contentView addSubview:_xmLabel];
    [self.xmLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(3 * AUTO_HEIGHT);
        make.left.mas_equalTo(15.00 * AUTO_WIDTH);
        make.size.mas_equalTo(CGSizeMake(6*18.00*AUTO_WIDTH, 25.00*AUTO_HEIGHT));
    }];
    self.xmLabel.text = @"风格要求";
    
    
    UIImageView *imageL = [UIImageView new];
    imageL.image = [UIImage imageNamed:@"虚线"];
    [self.contentView addSubview:imageL];
    [imageL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_xmLabel.mas_bottom).offset(4 * AUTO_HEIGHT);
        make.left.mas_equalTo(8 * AUTO_WIDTH);
        make.right.mas_equalTo(self.contentView.mas_right).offset(- 8 * AUTO_WIDTH);
        make.height.mas_equalTo(1.5 * AUTO_HEIGHT);
        
    }];
    
    
    
    _arr = [NSMutableArray arrayWithObjects:@"h1",@"h2",@"h3",@"h4",@"h5",@"h6",@"h7",@"h8",@"h9",@"h10",@"h11", @"h12",nil];
    _didArray = [NSMutableArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12", nil];
    _keyStrArray = [NSMutableArray arrayWithObjects:@"美式乡村",@"欧式古典",@"时尚混搭",@"北欧",@"中式",@"法式",@"日式",@"现代",@"田园",@"东南亚",@"地中海",@"其他", nil];
    //    for (NSInteger i = 0 ; i < _arr.count; i++) {
    //        _imageV= [[UIImageView alloc] initWithFrame:CGRectMake(15 * AUTO_WIDTH + i % 4 * 250/3 * AUTO_WIDTH, 40 + i / 4 * 250/3 * AUTO_HEIGHT, 230/3 * AUTO_WIDTH, 230/3 * AUTO_HEIGHT)];
    //       _imageV.tag = (1000 + i);
    //        NSString *name = [_arr objectAtIndex:i];
    //        _imageV.image = [UIImage imageNamed:name];
    //        [self.contentView addSubview:_imageV];
    //        _imageV.userInteractionEnabled = YES;
    //
    //        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    //        [_imageV addGestureRecognizer:tap];
    //
    //    }
    self.layout = [[UICollectionViewFlowLayout alloc] init];
    _margin = 5 * AUTO_HEIGHT;
    _itemWH = (SCREEN_WIDTH - 20 * AUTO_WIDTH-2 * _margin - 7) / 3 - _margin;
    self.layout.itemSize = CGSizeMake(327/4 * AUTO_WIDTH  , 225/4  * AUTO_HEIGHT+22 *AUTO_HEIGHT);
    self.layout.minimumInteritemSpacing = 10 * AUTO_WIDTH;
    self.layout.minimumLineSpacing = 10 * AUTO_WIDTH;
    _myCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(10 * AUTO_WIDTH, 45 * AUTO_WIDTH, SCREEN_WIDTH-20 *AUTO_WIDTH, _itemWH * 3 - 45 * AUTO_HEIGHT) collectionViewLayout:self.layout];
    
    //    裁掉view的边界
    //    裁掉view的边界
    self.myCollectionView.layer.masksToBounds = NO;
    //    设置半径
    //self.myCollectionView.layer.cornerRadius = 6 * AUTO_WIDTH;
    self.myCollectionView.scrollEnabled = NO;
    self.myCollectionView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.myCollectionView];
    [_myCollectionView registerClass:[YHYDetailPictureCollectionViewCell class] forCellWithReuseIdentifier:@"YHYDetailPictureCollectionViewCell"];
    //    self.myCollectionView.backgroundColor=[UIColor redColor];
    //    _myCollectionView.contentInset = UIEdgeInsetsMake(5 * AUTO_WIDTH, 5 * AUTO_HEIGHT, 5* AUTO_WIDTH, 5 * AUTO_HEIGHT);
    //    _myCollectionView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 0, -2 * AUTO_HEIGHT);
    
    _myCollectionView.dataSource = self;
    _myCollectionView.delegate = self;
    
    
    //    [_myCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.top.mas_equalTo(48 * AUTO_HEIGHT);
    //        make.left.mas_equalTo(15 * AUTO_WIDTH);
    //        make.width.mas_equalTo(SCREEN_WIDTH - 30 * AUTO_WIDTH);
    //        make.height.mas_equalTo( _itemWH * 3 - 48 * AUTO_HEIGHT);
    //
    //
    //    }];
    
    
    [_myCollectionView reloadData];
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _arr.count;
    
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    YHYDetailPictureCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"YHYDetailPictureCollectionViewCell" forIndexPath:indexPath];
    if (_picttureModelStr != nil) {
        
        NSString *picStr ;
        for (int i = 0; i < _keyStrArray.count; i++) {
            NSString *str = _keyStrArray[i];
            if ([str isEqualToString:_picttureModelStr]) {
                picStr = [NSString stringWithFormat:@"%@",str];
                UIImage *image = [_didArray objectAtIndex:[_keyStrArray indexOfObject:picStr]];
                _arr[[_keyStrArray indexOfObject:picStr]] = image;
                if (_keyArray != nil) {
                    _arr = [NSMutableArray arrayWithObjects:@"h1",@"h2",@"h3",@"h4",@"h5",@"h6",@"h7",@"h8",@"h9",@"h10",@"h11", @"h12",nil];
                    cell.myImageView.image = [UIImage imageNamed:[_arr objectAtIndex:indexPath.row]];
                    //                       _pictureName = picStr;
                }
                else{
                    
                    cell.myImageView.image = [UIImage imageNamed:[_arr objectAtIndex:indexPath.row]];
                }
                
            }
            
        }
        
        
        
        
    }
    else{
        
        cell.myImageView.image = [UIImage imageNamed:[_arr objectAtIndex:indexPath.row]];
        
    }
    
    
    
    
    if (_keyArray!= nil) {
        
        if ([_keyArray containsObject:cell.myImageView.image]) {
            
            [cell changeImage:YES];
            
            cell.myImageView.image = [UIImage imageNamed:[_didArray objectAtIndex:indexPath.row]];
            
        } else {
            [cell changeImage:NO];
            
        }
    }
    
    
    
    
    
    
    
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    YHYDetailPictureCollectionViewCell *cell = (YHYDetailPictureCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    
    _pictureName = _keyStrArray[indexPath.row];
    _keyArray = [NSMutableArray array];
    if ([_keyArray containsObject:cell.myImageView.image]) {
        
        [_keyArray removeObject:cell.myImageView.image];
        //[_keyArray removeAllObjects];
        cell.myImageView.alpha = 0;
        
    }
    else{
        cell.myImageView.alpha = 1;
        [_keyArray addObject:cell.myImageView.image];
        
        
    }
    
    [_myCollectionView reloadData];
    
    [self.myDelegate presentStr:_pictureName];
    
}



- (void)setPictureName:(NSString *)pictureName{
    _pictureName = pictureName;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
